package com.xr.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xr.blog.dao.BlogArticleMapper;
import com.xr.blog.pojo.BlogArticle;
import com.xr.blog.service.BlogArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;


@Service
public class BlogArticleServiceImpl extends ServiceImpl<BlogArticleMapper, BlogArticle> implements BlogArticleService {

    @Autowired
    BlogArticleMapper articleMapper;
    @Override
    public List<BlogArticle> findRandomArticles(Integer count) {
        return articleMapper.selectRandomArticles(count);
    }

    @Override
    public List<BlogArticle> findAllTopArticles() {
        return articleMapper.findAllTopArticles();
    }

    @Override
    public IPage<BlogArticle> findPageByKeyword(Page<BlogArticle> page, String keyword) {
        return articleMapper.findPageByKeyword(page,keyword);
    }

    @Override
    public List<BlogArticle> findHotArticles(Integer count) {

        return list(new QueryWrapper<BlogArticle>().select("id","url","title","visits")
                    .eq("status",1).eq("type",0)
                    .orderByDesc("visits").last("limit "+count)
        );
    }

    @Override
    public BlogArticle findArticleByUrl(String url) {
        BlogArticle blogArticle=articleMapper.findArticleByUrl(url);
        if(blogArticle!=null){
            articleMapper.updateForVisitsById(blogArticle.getId());
        }
        return blogArticle;
    }

    @Override
    public BlogArticle findPreviousById(Long id) {
        return getOne(new QueryWrapper<BlogArticle>().select("id","url","title")
                .eq("status",1).eq("type",0)
                .lt("id",id).last("limit 1")
        );
    }

    @Override
    public BlogArticle findNextById(Long id) {
        return getOne(new QueryWrapper<BlogArticle>().select("id","url","title")
                .eq("status",1).eq("type",0)
                .gt("id",id).last("limit 1")
        );
    }

    @Override
    public IPage<BlogArticle> findPageByCategory(Page<BlogArticle> page, Long categoryId) {
        return articleMapper.selectListByCategory(page,categoryId);
    }

    @Override
    public IPage<BlogArticle> findPageByTag(Page<BlogArticle> page, Long tagId) {
        return articleMapper.selectListByTag(page,tagId);
    }

    @Override
    public List<BlogArticle> findLatestArticle(Integer size) {
        return articleMapper.selectLatestArticle(size);
    }

    @Override
    public IPage<BlogArticle> findArticleByCondition(Page page, String title, Long categoryId, Integer status, Integer type) {
        return articleMapper.findArticleByCondition(page, title, categoryId, status, type);
    }

    @Override
    public BlogArticle findArticleById(Long articleId) {
        return baseMapper.selectById(articleId);
    }
}